﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;

namespace Heroius.NeuTabDiff
{
    public class RecordCollection
    {
        public void DefineExtProps(int keyidx, int grpidx, params string[] propNames)
        {
            if (keyidx == -1)
            {
                throw new ArgumentOutOfRangeException("表定义缺少 开课号");
            }

            if (grpidx == -1)
            {
                throw new ArgumentOutOfRangeException("表定义缺少 开课单位");
            }

            KeyIndex = keyidx;
            GroupIndex = grpidx;
            PropNames = propNames;
            PropValues = new List<Record>();
        }

        public void DefineExtProps(RecordCollection collection)
        {
            DefineExtProps(collection.KeyIndex, collection.GroupIndex, collection.PropNames);
        }

        public void Add(params string[] propVals)
        {
            if (propVals.Length != PropNames.Length)
            {
                throw new ArgumentException();
            }

            PropValues.Add(new Record(this, propVals));
        }

        internal int KeyIndex, GroupIndex;
        internal string[] PropNames;
        public List<Record> PropValues { get; set; }

        public void UpdateDg(DataGrid dg, TextBlock tb)
        {
            dg.Columns.Clear();
            foreach (string p in PropNames)
            {
                dg.Columns.Add(new DataGridTextColumn() { Header = p, Binding = new Binding($"[{p}]") });
            }
            dg.ItemsSource = PropValues;
            dg.UpdateLayout();
            tb.Text = PropValues.Count.ToString();
        }
    }

    public class RecordComparer : IEqualityComparer<Record>
    {
        public bool Equals(Record x, Record y)
        {
            return x.KeyValue == y.KeyValue;
        }

        public int GetHashCode(Record obj)
        {
            return obj.KeyValue.GetHashCode();
        }
    }
}
